#include "games/hovergame.h"

HoverGame::HoverGame(QWidget *parent, int difflvl) :
    BaseGame(parent, difflvl)
{
    n_rows = 10 + 2 * difficulty_level;
    n_cols = 10 + 2 * difficulty_level;

    map = new QCheckBox**[n_rows];
    for(int r = 0; r < n_rows; r++) {
        map[r] = new QCheckBox*[n_cols];

        for(int c = 0; c < n_cols; c++) {
            QCheckBox * p = new QCheckBox(this);
            map[r][c] = p;
            p->installEventFilter(this);
            p->setEnabled(false);
            lay->addWidget(p, r, c);
        }
    }
    curr_row = qrand() % n_rows;
    curr_col = qrand() % n_cols;
    current_check = map[curr_row][curr_col];
    current_check->setChecked(true);
    enableNeighbourhoods(curr_row, curr_col);
}

bool HoverGame::isSolved()
{
    if(curr_row > 0) {
        if(curr_col > 0 && !map[curr_row-1][curr_col-1]->isChecked()) return false;
        if(!map[curr_row-1][curr_col]->isChecked()) return false;
        if(curr_col < n_cols - 1 && !map[curr_row-1][curr_col+1]->isChecked()) return false;
    }
    if(curr_col > 0 && !map[curr_row][curr_col-1]->isChecked()) return false;
    if(curr_col < n_cols - 1 && !map[curr_row][curr_col+1]->isChecked()) return false;
    if(curr_row < n_rows - 1) {
        if(curr_col > 0 && !map[curr_row+1][curr_col-1]->isChecked()) return false;
        if(!map[curr_row+1][curr_col]->isChecked()) return false;
        if(curr_col < n_cols - 1 && !map[curr_row+1][curr_col+1]->isChecked()) return false;
    }
    return true;
}

bool HoverGame::eventFilter(QObject *object, QEvent * event)
{
    if( static_cast<QCheckBox*>(object) == current_check && event->type() ==  QEvent::Enter) {
        current_check->setChecked(false);
        disableNeighbourhoods(curr_row, curr_col);
        curr_row = qrand() % n_rows;
        curr_col = qrand() % n_cols;
        current_check = map[curr_row][curr_col];
        current_check->setChecked(true);
        enableNeighbourhoods(curr_row, curr_col);
        return true;
    }
    return false;
}

void HoverGame::enableNeighbourhoods(int r, int c)
{
    if(r > 0) {
        if(c > 0) map[r-1][c-1]->setEnabled(true);
        map[r-1][c]->setEnabled(true);
        if(c < n_cols - 1) map[r-1][c+1]->setEnabled(true);
    }
    if(c > 0) map[r][c-1]->setEnabled(true);
    if(c < n_cols - 1) map[r][c+1]->setEnabled(true);
    if(r < n_rows - 1) {
        if(c > 0) map[r+1][c-1]->setEnabled(true);
        map[r+1][c]->setEnabled(true);
        if(c < n_cols - 1) map[r+1][c+1]->setEnabled(true);
    }
}

void HoverGame::disableNeighbourhoods(int r, int c)
{
    if(r > 0) {
        if(c > 0) { map[r-1][c-1]->setChecked(false); map[r-1][c-1]->setEnabled(false); }
        map[r-1][c]->setChecked(false); map[r-1][c]->setEnabled(false);
        if(c < n_cols - 1) { map[r-1][c+1]->setChecked(false); map[r-1][c+1]->setEnabled(false); }
    }
    if(c > 0) { map[r][c-1]->setChecked(false); map[r][c-1]->setEnabled(false); }
    if(c < n_cols - 1) { map[r][c+1]->setChecked(false); map[r][c+1]->setEnabled(false); }
    if(r < n_rows - 1) {
        if(c > 0) { map[r+1][c-1]->setChecked(false); map[r+1][c-1]->setEnabled(false); }
        map[r+1][c]->setChecked(false); map[r+1][c]->setEnabled(false);
        if(c < n_cols - 1) { map[r+1][c+1]->setChecked(false); map[r+1][c+1]->setEnabled(false); }
    }
}
